home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: typedef double Poly[MAXPOLY] PROBLEM
- Date: 12 Jan 1996 04:02:50 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d4mha$pf7@news.iag.net>
- References: <4d2leh$14dm@pulp.ucs.ualberta.ca>
- NNTP-Posting-Host: pm3-orl6.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4d2leh$14dm@pulp.ucs.ualberta.ca>, ryangall@gpu.srv.ualberta.ca
- says...
- >
- >how can I send a (pointer to Poly) to a function, and access each index
- >for updating? this is my definition....its for school, and the typedef
- >double poly[MAXDEGREE] cannot be changed.....heres an example of one of
- >the functions I tried. It bails out at n=4 ...probably cause thats the
- >size of the pointer......it keeps crashing
- >
- >
- >#define MAXDEGREE 200
- >
- >typedef double Poly[MAXDEGREE];
- >typedef Poly * POLY;
- >
- >/* initialize the Poly P so all index's are 0 */
- >
- >int initPoly(POLY P)
- >{
- > int n;
- > for(n=0; n<MAXDEGREE; n++)
- > {
- > *P[n]=0.0; /* can you see what Im trying to do here?!*/
-
- Your problem is with operator precedence. The array member operator '[]'
- has a higher precedence than the dereference operator '*'. So this is
- equivalent to:
-
- *(P[n])=0.0;
-
- Use a debugger to step through your loop. Watch the following values:
-
- n /* your index */
- (*P) /* or P[0]: the address of the array you defined in main */
- P[n] /* or &(*P[n]): the address you are current trying to dereference */
- <You can fake this by inserting strategic printfs and getchars in the loop>
-
- On the second iteration (when n == 1). Subtract the value of (*P) from
- P[n]. You will end up with a figure equal to 200 * sizeof(double). You
- are literally indexing across arrays of 200 doubles, instead of through the
- elements of your intended array. So, after the first assignment, you are
- overwriting something you don't intend to (probably part of your stack).
-
- Use parens to force the precedence you need (ie, dereference P, to obtain
- your Poly, then index it).
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-